﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace NetFlux
{
    [Serializable]
    public class Server
    {
        public string Name{ get; private set; }
        public string Location { get; private set; }
        public IPAddress IP{get; private set;}

        public Server()
        {
            this.Name = "Test";
            this.IP = IPAddress.Parse("127.0.0.1");
            this.Status = "Indeterminate";
            this.Location = "Local";
        }

        public Server(string name,string location, string ip)
        {
            this.Name = name;
            this.IP = IPAddress.Parse(ip);
            this.Status = "Indeterminate";
            this.Location = location;
        }

        public Server(string name, string location, string ip, int port)
        {
            this.Name = name;
            this.IP = IPAddress.Parse(ip);
   
            this.Status = "Indeterminate";
            this.Location = location;
        }


        private bool IsServerOnline()
        {
            bool alive = false;
            TcpClient client;
            try
            {
                client = new TcpClient(IP.ToString(), 80);
                alive = true;
                try
                {
                    client.Close();
                }
                catch
                {
                }
            }
            catch(Exception e)
            {
                string s = e.Message;

            }
            return alive;
        }

        public void UpdateStatus()
        {
            bool online = IsServerOnline();
            if (online)
                this.Status = "Online";
            else
                this.Status = "Offline";
        }

        public string Status { get; private set; }
    }

    public static class ServerList
    {
        
        public static List<Server> Servers;
        public static void Init()
        {
            Servers = new List<Server>();
           
            //Ocelot
            Server ocelot = new Server("Ocelot", "USA", "197.85.186.71");
           
            Servers.Add(ocelot);         
        }

        public static void Export(string filename)
        {
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            var s = System.IO.File.OpenWrite(filename);
            serializer.Serialize(s, Servers);
            s.Close();
        }

        public static void Import(string filename)
        {         

            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            var s = System.IO.File.OpenRead(filename);
            Servers = serializer.Deserialize(s) as List<Server>;
            s.Close();
            
        }
    }
}
